package com.wzh.example.demo;

import com.wzh.example.demo.domain.UserDO;
import com.wzh.example.demo.repository.mapper.UserRepository;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * @Author zh wang
 * @Create 2025/4/15 11:45
 */
@Service
public class EntityManagerService {
    @PersistenceContext
    private EntityManager entityManager;
    @Autowired
    private DemoService demoService;
    @Autowired
    private UserRepository userRepository;
    @Transactional()
    public void test(UserDO user) {
        System.out.println("test 开始");
        updateDepartment("111");
        this.sleep(3*1000);
        update("111");
        System.out.println("test 结束");
    }
    @Transactional
    public void test2(UserDO user) {
        System.out.println("test2 开始");
        update("222");
        this.sleep(3*1000);
        updateDepartment("222");
        System.out.println("test2 结束");
    }
    @Transactional
    public void test3() {
        System.out.println("test3 开始");
        update("22");
        ((EntityManagerService)(AopContext.currentProxy())).update2("22");
        System.out.println("test3 结束");
    }
    @Async
    public void test4() {
        System.out.println("test4 开始");
        this.sleep(3*1000);
        //((EntityManagerService)(AopContext.currentProxy())).update2("44");
        demoService.updateTable2();
        System.out.println("test4 结束");
    }


    public void update(String email) {
        entityManager
                .createNativeQuery("update users set email='"+email+"' where userName='张三'")
                .executeUpdate();
    }
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void update2(String email) {
        entityManager
                .createNativeQuery("update users set email='"+email+"' where userName='张三'")
                .executeUpdate();
    }

    public void updateDepartment(String s) {
        entityManager
                .createNativeQuery("update department set description='负责公司产品研发和技术支持"+s+"' where name='研发部'")
                .executeUpdate();
    }

    public void addUser(UserDO user) {
        // 保存实体
        entityManager.persist(user);
    }

    public void create(){
        entityManager
                .createNativeQuery("drop table if exists test01 ")
                .executeUpdate();
        entityManager
                .createNativeQuery("create table test01 (id int primary key auto_increment,userName varchar(255),email varchar(255))")
                .executeUpdate();
    }
    private void sleep(int i) {
        try {
            Thread.sleep(i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }



    public void newUsers() {
        UserDO userDO = new UserDO();
        userDO.setUserName("王五2");
        userDO.setEmail("wangwu@1632.com");
        userRepository.save(userDO);
        System.out.println("userDO.getId() = " + userDO.getId());
    }
}
